// pages/home/home.js
const API = require('../../API/api.js')
var app = getApp()
Page({

   /**
    * 页面的初始数据
    */
   data: {
      screenWidth: 350,
      serverUrl: app.serverUrl,
      videoList: [],
      page: 1,
      totalPage: 1,
      pageSize: 10,
      videoDesc: ''
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      var me = this
      //获取手机的屏幕宽度并赋值 
      var screenWidth = wx.getSystemInfoSync().screenWidth / 2;
      var videoDesc = options.value
      me.setData({
         screenWidth: screenWidth,
         videoDesc: videoDesc
      })
      var page = this.data.page
      var isSaveRecord = 0
      if (videoDesc != null && videoDesc != "" && videoDesc != undefined) {
         isSaveRecord = 1
      }
      me.getAllVideos(page, isSaveRecord)
   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
      var me = this
      //获取当前的分页数 
      var page = 1;
      me.getAllVideos(page, 0)
   },


   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {
      var me = this
      var currentPage = me.data.page
      if (currentPage === me.data.totalPage) {
         wx.showToast({
            title: '没有更多数据了',
            icon: "none"
         })
         return
      }
      var page = currentPage + 1;
      me.getAllVideos(page, 0)
   },
   showVideoInfo: function (res) {
      var video = this.data.videoList[res.target.dataset.arrindex]
      console.log(video)
      wx.redirectTo({
         url: '../video/video?video=' + JSON.stringify(video)
      })
   },


   getAllVideos: function (page, isSaveRecord) {
      wx.showLoading({
         title: '加载中...',
      })
      var me = this
      var pageSize = me.data.pageSize;
      var videoDesc = me.data.videoDesc;

      API.videos({ videoDesc: videoDesc }, page, pageSize, isSaveRecord).then(
         res => {
            console.log(res)
            wx.hideLoading()
            //停止下拉刷新 就是上面那三个点的动画需要结束 
            wx.stopPullDownRefresh()
            var page = res.pageNum
            if (page === 1) {
               me.setData({
                  videoList: []
               })
            }
            var newList = res.list
            var videoList = me.data.videoList
            me.setData({
               videoList: videoList.concat(newList),
               totalPage: res.pages,
               page: page
            })

         }
      )

      // wx.request({
      //    url: app.serverUrl + '/video/list ?pageNum=' + page + "&pageSize=" + pageSize + "&isSaveRecord=" + isSaveRecord,
      //    method: "POST",
      //    //   header: {
      //    //     'content-type': 'application/x-www-form-urlencoded'
      //    //   },
      //    data: {
      //       videoDesc: videoDesc,

      //    },
      //    success: function (res) {
      //       console.log(res)
      //       wx.hideLoading()
      //       //停止下拉刷新 就是上面那三个点的动画需要结束 
      //       wx.stopPullDownRefresh()
      //       var page = res.data.data.pageNum
      //       if (page === 1) {
      //          me.setData({
      //             videoList: []
      //          })
      //       }
      //       var newList = res.data.data.list
      //       var videoList = me.data.videoList
      //       me.setData({
      //          videoList: videoList.concat(newList),
      //          totalPage: res.data.data.pages,
      //          page: page
      //       })

      //    }
      // })
   }

})